package com.lianxueda.drawing.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 应用程序配置类，配置静态资源处理、拦截器和跨域设置
 * @author 29264
 */
@Configuration
public class AppConfig implements WebMvcConfigurer {


    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 基于 StpUtil.checkLogin() 的登录校验拦截器，并且排除了/api/user/login接口用来开放登录（除了/api/user/login以外的所有接口都需要登录才能访问）。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/api/**")
                .excludePathPatterns(
                        "/api/v1/auth/login",
                        "/api/v1/auth/sendCode",
                        "/api/v1/auth/checkCode",
                        "/api/v1/captcha/**",
                        "/api/mp/**",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/swagger-ui/index.html",
                        "/favicon.ico");

    }
}
